using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    public class clsCHI_TIET_ND_GT
    {
        public clsCHI_TIET_ND_GT(string sConn)
        {
            SqlHelper.ConnectionString = sConn;
        }
        #region Private methods
        private CHI_TIET_ND_GT GetDataRow(DataRow row)
        {
            CHI_TIET_ND_GT objCHI_TIET_ND_GT = new CHI_TIET_ND_GT();
            try
            {
                if (row["MaChiTiet"] != DBNull.Value)
                    objCHI_TIET_ND_GT.MaChiTiet = long.Parse(row["MaChiTiet"].ToString());
            }
            catch { }
            try
            {
                if (row["MaNguoiDung"] != DBNull.Value)
                    objCHI_TIET_ND_GT.MaNguoiDung = ((row["MaNguoiDung"]).ToString());
            }
            catch { }
            try
            {
                if (row["MaGiaoTac"] != DBNull.Value)
                    objCHI_TIET_ND_GT.MaGiaoTac = row["MaGiaoTac"].ToString();
            }
            catch { }
            try
            {
                if (row["TenBangDuLieu"] != DBNull.Value)
                    objCHI_TIET_ND_GT.TenBangDuLieu = row["TenBangDuLieu"].ToString();
            }
            catch { }
            try
            {
                if (row["MaGiaoDich"] != DBNull.Value)
                    objCHI_TIET_ND_GT.MaGiaoDich = row["MaGiaoDich"].ToString();
            }
            catch { }
            try
            {
                if (row["MaChiTietGiaoDich"] != DBNull.Value)
                    objCHI_TIET_ND_GT.MaChiTietGiaoDich = int.Parse(row["MaChiTietGiaoDich"].ToString());
            }
            catch { }
            try
            {
                if (row["NgayThucHien"] != DBNull.Value)
                    objCHI_TIET_ND_GT.NgayThucHien = Convert.ToDateTime(row["NgayThucHien"]);
            }
            catch { }
            try
            {
                if (row["DuLieu"] != DBNull.Value)
                    objCHI_TIET_ND_GT.DuLieu = (row["DuLieu"].ToString());
            }
            catch { }


            return objCHI_TIET_ND_GT;
        }
        private List<CHI_TIET_ND_GT> GetCHI_TIET_ND_GT(DataTable dt)
        {
            List<CHI_TIET_ND_GT> lst = new List<CHI_TIET_ND_GT>();
            CHI_TIET_ND_GT obj;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                obj = this.GetDataRow(dt.Rows[i]);
                lst.Add(obj);
            }
            return lst;
        }
        #endregion Private methods

        #region Public methods
        public List<CHI_TIET_ND_GT> usp_SelectCHI_TIET_ND_GT_byMaGiaoDich(string MaGiaoDich)
        {
            string spName = "usp_SelectCHI_TIET_ND_GT_byMaGiaoDich";
            DataTable dt = new DataTable();
            List<CHI_TIET_ND_GT> lst = new List<CHI_TIET_ND_GT>();
            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaGiaoDich", MaGiaoDich);             


                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, par);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, par);
                }
                lst = this.GetCHI_TIET_ND_GT(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lst;
        }
        public int usp_InsertCHI_TIET_ND_GT(CHI_TIET_ND_GT obj)
        {
            int iResult = 0;
            string spName = "usp_InsertCHI_TIET_ND_GT";

            try
            {
                SqlParameter[] par = new SqlParameter[7];
                par[0] = new SqlParameter("@MaNguoiDung", obj.MaNguoiDung);
                par[1] = new SqlParameter("@MaGiaoTac", obj.MaGiaoTac);
                par[2] = new SqlParameter("@TenBangDuLieu", obj.TenBangDuLieu);
                par[3] = new SqlParameter("@MaGiaoDich", obj.MaGiaoDich);
                par[4] = new SqlParameter("@MaChiTietGiaoDich", obj.MaChiTietGiaoDich);
                par[5] = new SqlParameter("@NgayThucHien", obj.NgayThucHien);
                par[6] = new SqlParameter("@DuLieu", obj.DuLieu);
                


                if (SqlHelper.IsHaveTran())
                    iResult = SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, par);
                else
                    iResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, spName, par);

            }
            catch (Exception ex)
            {
            }
            return iResult;
        }
        #endregion
    }
}
